import { ElMessage } from 'element-plus'

type MessageType = 'success' | 'warning' | 'error'
type MessageCallback = () => void

const showMessage = (msg: string, callback?:MessageCallback, type?:MessageType) => {
  ElMessage({
    type: type,
    message: msg,
    duration: 2000,
    onClose: () => {
      if (callback) {
        callback()
      }
    }
  })
}

const message = {
  error: (msg: string, callback?: MessageCallback) => {
    showMessage(msg, callback, 'error');
  },
  success: (msg: string, callback?: MessageCallback) => {
    showMessage(msg, callback, 'success');
  },
  warning: (msg: string, callback?: MessageCallback) => {
    showMessage(msg, callback, 'warning');
  },
};

export default message